package com.soccertgc.core.game
{
	import com.soccertgc.utils.ClassUtils;
	
	public class GameSide
	{
			
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------              
		
		public static const NONE:GameSide                 	= new GameSide(0, "none");
		public static const HOME:GameSide                 	= new GameSide(1, "home");
		public static const AWAY:GameSide                	= new GameSide(2, "away");
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<GameSide>;
		
		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<GameSide>();
			_list.push(GameSide.NONE);
			_list.push(GameSide.HOME);
			_list.push(GameSide.AWAY);
			// Init!
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:uint;
		private var _name:String;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function GameSide(id:uint, name:String)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("GameSide enum already created!");
			// Init
			_id    = id;
			_name  = name;
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------              
		
		public function get id():uint
		{
			return _id;
		}
		
		
		public function get name():String
		{
			return _name;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * Returns the list of the names of the game sides
		 * @return List of the names of the game sides
		 */
		public static function names():Array
		{
			var list:Array  = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i].name);
			}
			return list;
		}
		
		/**
		 * 
		 * @return 
		 */
		public static function list():Array
		{
			var list:Array = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i]);
			}
			return list;
		}
		
		/**
		 * Search a GameSide by given id 
		 * @param id Id of the log target to find
		 * @return GameSide constant for the given id, null if no GameSide
		 * for the given id has been found
		 */             
		public static function getById(id:uint):GameSide
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("GameSide for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a GameSide by given name 
		 * @param name Name of the log target to find
		 * @return GameSide constant for the given id, null if no GameSide
		 * for the given name has been found
		 */             
		public static function getByName(name:String):GameSide
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("GameSide for name '" + name + "' unknown!");
		}
			
	}

}